 require('../css/personal.less');
 document.ready(function(){
    let showgenderPicker=document.querySelector('#showgenderPicker');
    let showDatePicker=document.querySelector('#showDatePicker');
    let  showprovincePicker= document.querySelector('#showprovincePicker');
    let  showcityPicker=document.querySelector('#showcityPicker');
    let gender=document.querySelector('#gender');
    let birthday=document.querySelector('#birthday');
    let  prosheng=document.querySelector('#prosheng');
    let city=document.querySelector("#city");
    let individual=document.querySelector('#individual');
    let individual_num1=document.querySelector('#individual_num1');
    let back1=document.querySelector("#back");
    let nickname1=document.querySelector('#js_input')
    let message=document.querySelector('#message');
    console.log(nickname1)
     
    back1.addEventListener("click",function(){
        window.history.go(-1)
    })
  individual.addEventListener('input',function(){
    individual_num1.textContent= individual.value.length;
  })
   //这是选择性别区域
    showgenderPicker.addEventListener('click',function(){

         weui.picker([
     
    {
        label: '男',
     
        value: 0
    }, {
        label: '女',
        value: 0
    }], {
        // onChange: function (result) {
        //     console.log(result);
        // },
        onConfirm: function (result) {
            gender.textContent=result[0].label
        },
        title: '请选择性别'
    });
    })
    //这是选择生日区域
    showDatePicker.addEventListener('click',function(){

        weui.datePicker({
            start: 1980,
            end: new Date().getFullYear(),
          
            onConfirm: function (result) {
                // console.log(result);
                
      let  arrDate= result.map(function(date){
         
              console.log(date)
                return $$.birparty(date.value)
           }).join('-')
           
           birthday.textContent=arrDate
            },
            title: '多列选择器'
        });
    })
    //address/province
    //这是网服务器发送axios请求返回省份区域

    axios.get(window.$$.username+`/address/province`)
    .then(function(res){
    let ret=res.data 
//    console.log(res);
   
    if(ret.status===0){
    arrProvince=ret.data.map(function(proin){
    let  obj={
    label:proin.name,
    value:proin.addressId
    }
    return obj
    })
    }
 
    })
    .catch(function(error){
    console.log(error)
    })
    //这是定义的空数组区域
  let arrProvince=[];
   let cityArr=[];
   //这是选择省份区域
   showprovincePicker.addEventListener('click',function(){
         //选择省份
        weui.picker(

        arrProvince, {
        onConfirm: function (result) {
        prosheng.textContent=result[0].label;

        axios.get(window.$$.username+`/address/city/${result[0].value}`)
        .then(function(res){
        let ret=res.data;
        console.log(res);
        
         
        if(ret.status===0){
        cityArr=ret.data.map(function(cit){
        let   obj2={
            label:cit.name,
            value:cit.addressId
            }
            return obj2
        })

        }
        })
        .catch(function(error){
        console.log(error);

        })
        },
        title: '请选择省份'
        });

   })
 //这是选择城市区域

 showcityPicker.addEventListener('click',function(){
    if(cityArr.length>0){
        weui.picker(
      
            cityArr,{
            onChange: function (result) {
                city.textContent=result[0].label
            },
            onConfirm: function (result) {
                console.log(result);
            },
            title: '请选择城市'
        });
    }
   })
   //这是数据回显区域
  
    playBack()
   function playBack(){
       let use=JSON.parse(window.localStorage.getItem('user'));

       nickname1.value=use.nickname;
       gender.textContent=use.gender?use.gender:'请选择';
       birthday.textContent=use.birthday?$$.get(use.birthday):'请选择';
       prosheng.textContent=use.address?use.address.split(",")[0]:'请选择';
       city.textContent=use.address?use.address.split(',')[1]:'请选择';
   }
  //这是文本域的开始
  individual.addEventListener('input',function(){
    console.log(gender.textContent,birthday.textContent)
  })
  // 这是保存信息的数据回显区域的开始message;
  message.addEventListener('click',function(){
    let us= JSON.parse(window.localStorage.getItem('user')).userId
   
    axios.post(window.$$.username+`/users/userEdit`,
    {             
        "nickname":nickname1.value, 	
        "gender":gender.textContent,	
         "birthday":birthday.textContent,
          "address":[prosheng.textContent,city.textContent],
           "sign":individual.value,
           "userId":us
    })
    .then(function(res){
  
    if(res.data.status===0){
        $$.showmask('correct-1-mini',"保存成功")
        setTimeout(function(){
            window.location.href="./my.html";
        },3000)
    }
    })
    .catch(function(error){
        console.log(error)
    })
  })
    


 })